package cc.vicp.djx314.starfood.service.count.impls;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cc.vicp.djx314.starfood.dao.count.CountMapper;
import cc.vicp.djx314.starfood.domain.dish.Dish;
import cc.vicp.djx314.starfood.service.count.CountService;

@Service
public class CountServiceImpl implements CountService {
	public static final Logger logger = LoggerFactory.getLogger(CountServiceImpl.class);

	@Autowired
	CountMapper countMapper;

	@Override
	@Transactional(readOnly = true)
	public List<Dish> getTopDishByDay(Date date) {
		java.sql.Date myDate = new java.sql.Date(date.getTime());
		List<Dish> dishes = countMapper.getTopDishByDay(myDate, CountService.DISH_DAY_TOP_LIMIT);
		logger.info("统计{}菜式排名前{}", myDate, CountService.DISH_DAY_TOP_LIMIT);
		return dishes;
	}

	@Override
	@Deprecated
	public List<Dish> getTopDishByWeek(Date date) {
		java.sql.Date myDate = new java.sql.Date(date.getTime());
		List<Dish> dishes = countMapper.getTopDishByWeek(myDate, CountService.DISH_WEEK_TOP_LIMIT);
		return dishes;
	}

	@Override
	@Transactional(readOnly = true)
	public List<Dish> getTopDishLastWeek(Date date) {
		Date lastWeek = new Date(date.getTime() - 7 * 24 * 60 * 60 * 1000);
		Calendar lastWeekCal = Calendar.getInstance();
		lastWeekCal.setTime(lastWeek);
		lastWeekCal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
		java.sql.Date lastSunday = new java.sql.Date(lastWeekCal.getTimeInMillis());
		lastWeekCal.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
		java.sql.Date lastSaturday = new java.sql.Date(lastWeekCal.getTimeInMillis());
		List<Dish> dishes = countMapper.getTopDishByDateRange(lastSunday,
				lastSaturday, CountService.DISH_WEEK_TOP_LIMIT);
		logger.info("统计{}到{}一周菜式排名前{}", lastSunday, lastSaturday, CountService.DISH_WEEK_TOP_LIMIT);
		return dishes;
	}

	@Override
	@Transactional(readOnly = true)
	public List<Dish> getTopDishLastMonth(Date date) {
		Date today = new Date();
		Calendar lastMonthCal = Calendar.getInstance();
		lastMonthCal.setTime(today);
		lastMonthCal.add(Calendar.MONTH, - 1);

		lastMonthCal.set(Calendar.DAY_OF_MONTH, lastMonthCal.getActualMinimum(Calendar.DAY_OF_MONTH));
		java.sql.Date lastMonthFirst = new java.sql.Date(lastMonthCal.getTimeInMillis());

		lastMonthCal.set(Calendar.DAY_OF_MONTH, lastMonthCal.getActualMaximum(Calendar.DAY_OF_MONTH));
		java.sql.Date lastMonthLast = new java.sql.Date(lastMonthCal.getTimeInMillis());

		List<Dish> dishes = countMapper.getTopDishByDateRange(lastMonthFirst,
				lastMonthLast, CountService.DISH_MONTH_TOP_LIMIT);
		logger.info("统计{}到{}一月菜式排名前{}", lastMonthFirst, lastMonthLast, CountService.DISH_WEEK_TOP_LIMIT);
		return dishes;
	}

	@Override
	@Transactional(readOnly = true)
	public List<Map<String, Double>> getSalesLastWeek(Date date) {
		Date today = new Date();
		Calendar lastWeekCal = Calendar.getInstance();
		lastWeekCal.setTimeInMillis(today.getTime() - 7 * 24 * 60 * 60 * 1000);

		lastWeekCal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
		lastWeekCal.set(Calendar.HOUR_OF_DAY, 0);
		lastWeekCal.set(Calendar.MINUTE, 0);
		lastWeekCal.set(Calendar.SECOND, 0);
		Date start = new Date(lastWeekCal.getTimeInMillis());

		lastWeekCal.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
		lastWeekCal.set(Calendar.HOUR_OF_DAY, 23);
		lastWeekCal.set(Calendar.MINUTE, 59);
		lastWeekCal.set(Calendar.SECOND, 59);
		Date end = new Date(lastWeekCal.getTimeInMillis());

		List<Map<String, Double>> results = countMapper.getSalesByDateRange(start, end);
		logger.info("统计{}到{}一周销量", start, end);
		return results;
	}

	@Override
	public List<Map<String, Double>> getSalesLastMonth(Date date) {
		Date today = new Date();
		Calendar lastMonthCal = Calendar.getInstance();
		lastMonthCal.setTime(today);
		lastMonthCal.add(Calendar.MONTH, - 1);

		lastMonthCal.set(Calendar.DAY_OF_MONTH, lastMonthCal.getActualMinimum(Calendar.DAY_OF_MONTH));
		lastMonthCal.set(Calendar.HOUR_OF_DAY, 0);
		lastMonthCal.set(Calendar.MINUTE, 0);
		lastMonthCal.set(Calendar.SECOND, 0);
		Date start = new Date(lastMonthCal.getTimeInMillis());

		lastMonthCal.set(Calendar.DAY_OF_MONTH, lastMonthCal.getActualMaximum(Calendar.DAY_OF_MONTH));
		lastMonthCal.set(Calendar.HOUR_OF_DAY, 23);
		lastMonthCal.set(Calendar.MINUTE, 59);
		lastMonthCal.set(Calendar.SECOND, 59);
		Date end = new Date(lastMonthCal.getTimeInMillis());

		List<Map<String, Double>> results = countMapper.getSalesByDateRange(start, end);
		logger.info("统计{}到{}一月销量", start, end);
		return results;
	}
}